<?php
namespace Common\Model;
use Think\Model;
class NoticeModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_notice'; 
	
	public function getNoticeList($data){
		if(!empty($data['tcustid'])){
			$map['tcustid']  = $data['tcustid'];
		}
		
		if(!empty($data['moduleid'])){
			$map['moduleid']  = $data['moduleid'];
		}
		
		
		$total = $this->where($map)->count();
		$fields = array('noticeid','custid','tcustid','content',
				'moduleid','businessid','returnid','status','createtime',
				'updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "noticeid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	
	
	public function getNotice($data){
		$map['noticeid'] = $data['noticeid'];
		$result = $this->where($map)->find();
		return $result;
	}
	
	
	public function addNotice($data){
		$result = $this->add($data);
		return $result;
	}
	
	
	public function updateNotice($data){
		if(!empty($data['noticeids']) && is_array($data['noticeids']))$map['noticeid'] = array('in' , $data['noticeids']);
		if(!empty($data['noticeid']))$map['noticeid'] = $data['noticeid'];
		$map['tcustid'] = $data['tcustid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteNotice($data){
		if(!empty($data['noticeids']) && is_array($data['noticeids']))$map['noticeid'] = array('in' , $data['noticeids']);
		if(!empty($data['noticeid']))$map['noticeid'] = $data['noticeid'];
		$map['tcustid'] = $data['tcustid'];
		$result = $this->where($map)->delete($data);
		return $result;
	}
}


